﻿//-------------------------------------------------------------------
//	Copyright (c) 2010-2014  Zhirnov Andrey
//	This file is part of the "UX Standard Lib" project.
//	See copyright notice in "UX_STL_Core.h".
//-------------------------------------------------------------------

#ifndef _UXS_ALGEBRA_H
#define _UXS_ALGEBRA_H

#pragma once

#include "Mathematics.h"

namespace UX_STL
{
namespace UXMath
{

	//
	// Trigonometry
	//

	struct Trigonometry
	{
	};



	//
	// Pythagoras Theorem
	//

	struct Pythagoras
	{
		//                            |\ d
		// d = sqrt(x*x + y*y)  for x |_\
		//                             y
		template <typename T>
		static T Diagonal (const T& x, const T& y)
		{
			return Sqrt( Square( x ) + Square( y ) );
		}
		
		//                            |\ d
		// b = sqrt(d*d - a*a)  for b |_\
		//                             a
		template <typename T>
		static T Side (const T& a, const T& d)
		{
			ASSERT( Abs(a) < Abs(d) );
			return Sqrt( Square( d ) - Square( a ) );
		}
	};



	//
	// Linear equation
	//

	struct LinearEquation
	{
		// ax + b = 0
		template <typename T>
		static T  GetX (const T& a, const T& b = T(0))
		{
			return ( -b / a );
		}

		// ax + by + c = 0
		template <typename T>
		static T  GetY (const T& x, const T& a, const T& b, const T& c)
		{
			return ( a * x + c ) / -b;
		}

		// y = kx + m
		template <typename T>
		static T  GetY (const T& x, const T& k, const T& m)
		{
			return ( k*x + m );
		}
	};



	//
	// Quadratic equation
	//

	struct QuadraticEquation
	{
		// ax^2 + bx + c = 0
		template <typename T>
		static TVec<T,2>  GetX12 (const T& a, const T& b, const T& c = T(0))
		{
			ASSERT( IsNotZero( a ) );
			
			const T	disc		= b*b - T(4)*a*c;
			
			if ( disc < T(0) )
			{
				WARNING( "D < 0" );
				return TVec<T,2>( MaxValue<T>() );
			}

			const T	bb_4ac_2a	= Sqrt( disc ) / ( T(2) * a );
			const T	b_2a		= -b / ( T(2) * a );

			const T	x1			= ( b_2a + bb_4ac_2a ),
					x2			= ( b_2a - bb_4ac_2a );

			return TVec<T,2>( x1, x2 );
		}
	};

	
//-------------------------------------------------------------------

}	// UXMath
}	// UX_STL

#endif	// _UXS_ALGEBRA_H